import Vue from 'vue'
import VueRouter from 'vue-router'
import 'nprogress/nprogress.css'
import store from 'store'
import NProgress from 'nprogress'
import {Loading} from 'element-ui';


import {cookieStorage, localStorage, sessionStorage} from 'common/storage'
import {toolsVerify} from 'common/tools'
import {appKey, sso_url} from 'common/config';
//import components
import viewPageComponent from 'pages/App.vue'

import loginComponent from 'pages/login'
//home
import homeComponent from 'pages/home'
//404
import noPageComponent from 'pages/error/404'
import unauthorizedPageComponent from 'pages/error/unauthorized'
//舆论
import consensusComponent from 'pages/consensus'
import consensusDetailComponent from 'pages/consensus/consensusDetail'
import typeDetailComponent from 'pages/consensus/typeDetail'

import articleComponent from 'pages/article'
import articleDetailComponent from 'pages/article/articleDetail'

import commentComponent from 'pages/comment'

import reviewerComponent from 'pages/reviewer'
import reviewerDetailComponent from 'pages/reviewer/reviewerDetail'

import keywordComponent from 'pages/keyword'

import warningCreateComponent from 'pages/warning/create'
import warningComponent from 'pages/warning'
import warningResultComponent from 'pages/warning/result'


Vue.use(VueRouter)

const routes = [{
  path: '/404',
  name: 'notPage',
  component: noPageComponent
},
  {
    path: '/unauthorized',
    name: 'unauthorizedPage',
    component: unauthorizedPageComponent
  }, 
  {
    path: '/login',
    name: 'login',
    component: loginComponent,
    /*
      beforeEnter: (to, from, next) =>{
        var token = to.params.token;
        let loadingInstance1 = Loading.service({ fullscreen: true });
        router.app.$fetch.apiLogin.getUserInfo({token: token}).then(({data: result}) => {
          loadingInstance1.close();
          if(result.code === 1000){
            let expireDays = 1000 * 60 * 60 * 24;
            cookieStorage.set("sessionId", result.content.sessionId, {expires: expireDays});
            localStorage.set("loginInfo", result.content);
            return router.push({path: '/home' });

          }else {
            return router.push({name: 'unauthorizedPage', params: {desc: result.message}});
          }

        })
        //setTimeout(next(), 5000);
      }*/
  },
  /*{
      path: '/checkLogin',
      name: 'checkLogin',
      //redirect: 'https://sso-t.yaochufa.com/sso/login'
      //component: loginComponent
  }, */
  {
    path: '/',
    redirect: '/home',
    component: viewPageComponent,
    children: [{
      path: '/home',
      name: 'home',
      component: homeComponent,
      meta: {
        title: "主页",
        auth: true
      }
    }, {
      path: '/consensus',
      name: 'consensus',
      component: consensusComponent,
      meta: {
        title: "舆情列表",
        auth: true
      }
    }, {
      path: '/consensus/detail',
      name: 'consensusDetail',
      component: consensusDetailComponent,
      meta: {
        title: "舆情列表",
        auth: true
      }
    },
      {
        path: '/article',
        name: 'article',
        component: articleComponent,
        meta: {
          title: "文章列表",
          auth: true
        }
      }, {
        path: '/comment',
        name: 'comment',
        component: commentComponent,
        meta: {
          title: "评论列表",
          auth: true
        }
      }, {
        path: '/reviewer',
        name: 'reviewer',
        component: reviewerComponent,
        meta: {
          title: "受众列表",
          auth: true
        }
      }, {
        path: '/reviewer/detail',
        name: 'reviewerDetail',
        component: reviewerDetailComponent,
        meta: {
          title: "受众详情",
          auth: true
        }
      }, {
        path: '/article/detail',
        name: 'articleDetail',
        component: articleDetailComponent,
        meta: {
          title: "文章详情",
          auth: true
        }
      }, {
        path: '/keyword/keyword',
        name: 'keyword',
        component: keywordComponent,
        meta: {
          title: "关键词词云",
          auth: true
        }
      }, {
        path: '/topic/type',
        name: 'typeDetail',
        component: typeDetailComponent,
        meta: {
          title: "类型词云",
          auth: true
        }
      }, {
        path: '/warning/create',
        name: 'warningCreate',
        component: warningCreateComponent,
        meta: {
          title: "预警设置",
          auth: true
        }
      },
      {
        path: '/warning/index',
        name: 'warning',
        component: warningComponent,
        meta: {
          title: "预警列表",
          auth: true
        }
      },
      {
        path: '/warning/result',
        name: 'warning',
        component: warningResultComponent,
        meta: {
          title: "监督结果",
          auth: true
        }
      },
    ]
  }]
const router = new VueRouter({
  routes,
  mode: 'history', //default: hash ,history
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {x: 0, y: 0}
    }
  }
})

//router.beforeEach((to, from, next) =>{
// var checkLoginNoFlag = function(){
//   var userInfo = localStorage.get("loginInfo");
//   return toolsVerify.isEmptyObject(cookieStorage.get('sessionId')) || toolsVerify.isEmptyObject(userInfo);
//};
//if(to.name != 'login'){
// if(checkLoginNoFlag() && to.name != "unauthorizedPage"){
//重定向到sso登录页面
//   window.location.href = sso_url+"/login?loginType="+appKey+"&redirectUrl=cpa";
// }
//}
//next();
//});
//路由完成之后的操作
router.afterEach(() => {
  router.app.$store.dispatch('clearIntervals')
  router.app.$store.dispatch('clearTimeout')
})
export default router
